clear all
set more off
tempfile temp xxx

** PATHS
local ubigeo ../../ubigeo

** DATA
use `ubigeo'/ubigeo1993

sort coddpto codprov coddist
by coddpto: gen DEPTname = nombre[1]
by coddpto codprov: gen PROVname = nombre[1]
gen DISTname = nombre
replace DEPTname = trim(DEPTname)
replace PROVname = trim(PROVname)
replace DISTname = trim(DISTname)

drop if coddist == "00"
gen ubigeo = coddpto + codprov + coddist
drop coddpto codprov coddist nombre

replace DISTname = subinstr(DISTname,"¥","Ñ",.)
replace PROVname = subinstr(PROVname,"¥","Ñ",.)

gen y = _n
rename ubigeo ubigeo1993
save `temp', replace


use `ubigeo'/ubigeo2001, clear 
sort coddpto codprov coddist
by coddpto: gen DEPTname = nombre[1]
by coddpto codprov: gen PROVname = nombre[1]
gen DISTname = nombre
replace DEPTname = trim(DEPTname)
replace PROVname = trim(PROVname)
replace DISTname = trim(DISTname)

replace DISTname = subinstr(DISTname,"¥","Ñ",.)
replace PROVname = subinstr(PROVname,"¥","Ñ",.)

drop if coddist == "00"
gen ubigeo2001 = coddpto + codprov + coddist
drop coddpto codprov coddist nombre

keep ubigeo *name*
gen x = _n

reclink DEPTname PROVname DISTname using `temp', idmaster(x) idusing(y) gen(score)
tab _merge
destring ubigeo*, replace
gen ubigeohost = ubigeo2001
gsort -score ubigeohost
tab ubigeohost if _merge == 1

replace ubigeo1993 = 021801 if ubigeohost == 021809 & _merge == 1 // nuevo chimbote to old chimbote
replace ubigeo1993 = 030202 if ubigeohost == 030219 & _merge == 1 // nuevo chimbote to old chimbote
replace ubigeo1993 = 030501 if ubigeohost == 030506 & _merge == 1 // challhuahuacho to tambobamba
replace ubigeo1993 = 030601 if ubigeohost == 030608 & _merge == 1 // curimana to p abad
replace ubigeo1993 = 030706 if ubigeohost == 030714 & _merge == 1 // curasco to micaela bastidas
replace ubigeo1993 = 040112 if ubigeohost == 040129 & _merge == 1 // Jose L Bust. y Rivero to paucarpata
replace ubigeo1993 = 040511 if ubigeohost == 040520 & _merge == 1 // majes to lluta
replace ubigeo1993 = 050101 if ubigeohost == 050115 & _merge == 1 // jesus nazareno to ayacucho
replace ubigeo1993 = 050407 if ubigeohost == 050408 & _merge == 1 // llochegua to sivia
replace ubigeo1993 = 060307 if ubigeohost == 060312 & _merge == 1 // la libertad de pallan to miguel iglesias
replace ubigeo1993 = 060413 if ubigeohost == 060419 & _merge == 1 // chalamarca to paccha
replace ubigeo1993 = 080806 if ubigeohost == 080808 & _merge == 1 // alto pichigua to pichigua
replace ubigeo1993 = 090101 if ubigeohost == 090118 & _merge == 1 // ascension to huancavelica
replace ubigeo1993 = 100102 if ubigeohost == 100111 & _merge == 1 // pillco arca to amarilis
replace ubigeo1993 = 100302 if ubigeohost == 101104 & _merge == 1 // aparicio pomares to chupa
replace ubigeo1993 = 100306 if ubigeohost == 101108 & _merge == 1 // choras to chavinillo
replace ubigeo1993 = 130201 if ubigeohost == 130208 & _merge == 1 // casa grande to ascope
replace ubigeo1993 = 130609 if ubigeohost == 131103 & _merge == 1 // cormin
replace ubigeo1993 = 130112 if ubigeohost == 131202 & _merge == 1 // cguadalupito to old viru
replace ubigeo1993 = 130112 if ubigeohost == 131203 & _merge == 1 // viru to old viru
replace ubigeo1993 = 140115 if ubigeohost == 140116 & _merge == 1 // patapo to sana
replace ubigeo1993 = 140115 if ubigeohost == 140117 & _merge == 1 // pucala to sana
replace ubigeo1993 = 140115 if ubigeohost == 140119 & _merge == 1 // cayalti to sana
replace ubigeo1993 = 140101 if ubigeohost == 140118 & _merge == 1 // pomalca to cix
replace ubigeo1993 = 140101 if ubigeohost == 140120 & _merge == 1 // tuman to cix
replace ubigeo1993 = 160401 if ubigeohost == 160404 & _merge == 1 // san pablo to ramon castill
replace ubigeo1993 = 160507 if ubigeohost == 160510 & _merge == 1 // jenaro herrera to saquena
replace ubigeo1993 = 160201 if ubigeohost == 160706 & _merge == 1 // andoas
replace ubigeo1993 = 170101 if ubigeohost == 170104 & _merge == 1 // laberinto to tambopata
replace ubigeo1993 = 170201 if ubigeohost == 170204 & _merge == 1 // huetetopue to manu
replace ubigeo1993 = 211201 if ubigeohost == 211209 & _merge == 1 // alto inambaro to sandia
replace ubigeo1993 = 211207 if ubigeohost == 211210 & _merge == 1 // putina to san juan
replace ubigeo1993 = 230101 if ubigeohost == 230110 & _merge == 1 // albarracin to tacna
replace ubigeo1993 = 250102 if ubigeohost == 250106 & _merge == 1 // n requena to campo verde
replace ubigeo1993 = 250301 if ubigeohost == 250303 & _merge == 1 // curimana to p abad

tab ubigeohost if ubigeo1993 == .

gen dd = DISTname == UDISTname
gen flag = (score < 0.7 & _merge == 3 & dd == 0)
tab ubigeohost if flag == 1

replace ubigeo1993 = 030601 if ubigeohost == 030608 & flag == 1 // curimana to p abad
replace flag = 0 if ubigeohost == 030608 & flag == 1

replace ubigeo1993 = 040112 if ubigeohost == 040129 & flag == 1 // Jose L Bust. y Rivero to paucarpata
replace flag = 0 if ubigeohost == 040129 & flag == 1

replace ubigeo1993 = 040511 if ubigeohost == 040520 & flag == 1 // majes to lluta
replace flag = 0 if ubigeohost == 040520 & flag == 1

replace ubigeo1993 = 080907 if ubigeohost == 080910 & flag == 1 // pichari to kimbiri
replace flag = 0 if ubigeohost == 080910 & flag == 1

replace flag = 0 if ubigeohost == 120906 & flag == 1 // san juan de iscos
replace flag = 0 if ubigeohost == 120908 & flag == 1 // tres de diciembre
replace flag = 0 if ubigeohost == 131103 & flag == 1 // marmot

replace ubigeo1993 = 160101 if ubigeohost == 160112 & flag == 1  // belen to iquitos
replace flag = 0 if ubigeohost == 160112 & flag == 1

replace ubigeo1993 = 160101 if ubigeohost == 160113 & flag == 1 // san juan bautista to iquitos
replace flag = 0 if ubigeohost == 160113 & flag == 1

replace ubigeo1993 = 160109 if ubigeohost == 160114 & flag == 1 // tentiente.. to putumayo
replace flag = 0 if ubigeohost == 160114 & flag == 1

replace ubigeo1993 = 160507 if ubigeohost == 160510 & flag == 1 // jenaro herrera to saquena
replace flag = 0 if ubigeohost == 160510 & flag == 1

replace ubigeo1993 = 160209 if ubigeohost == 160706 & flag == 1  // andoas to pastaza
replace flag = 0 if ubigeohost == 160706 & flag == 1

replace ubigeo1993 = 160111 if ubigeohost == 160511 & flag == 1 // yaqyerana
replace flag = 0 if ubigeohost == 160511 & flag == 1

replace ubigeo1993 = 170101 if ubigeohost == 170104 & flag == 1 // laberinto to tambopata
replace flag = 0 if ubigeohost == 170104 & flag == 1

replace ubigeo1993 = 170201 if ubigeohost == 170204 & flag == 1 // huetetopue to manu
replace flag = 0 if ubigeohost == 170204 & flag == 1

replace ubigeo1993 = 210504 if ubigeohost == 210505 & flag == 1 // tentiente.. to putumayo
replace flag = 0 if ubigeohost == 210505 & flag == 1

replace flag = 0 if ubigeohost == 230402 & flag == 1 // albarracin to chucatami

replace ubigeo1993 = 250101 if ubigeohost == 250107 & flag == 1 // manantay to calleria
replace flag = 0 if ubigeohost == 250107 & flag == 1

replace ubigeo1993 = 250102 if ubigeohost == 250106 & flag == 1 // n requena to campo verde
replace flag = 0 if ubigeohost == 250106 & flag == 1

tab ubigeohost if flag == 1

keep ubigeo2001 ubigeo1993 
duplicates drop

** SAVE
save `ubigeo'/ubigeo2001to1993, replace















e
replace ubigeo1993 = 021801 if ubigeohost == 021809 & _merge == 1 // nuevo chimbote to old chimbote
replace ubigeo1993 = 030202 if ubigeohost == 030219 & _merge == 1 // nuevo chimbote to old chimbote
replace ubigeo1993 = 030501 if ubigeohost == 030506 & _merge == 1 // challhuahuacho to tambobamba
replace ubigeo1993 = 030605 if ubigeohost == 030608 & _merge == 1 // ranracancha to ocobamba
replace ubigeo1993 = 030706 if ubigeohost == 030714 & _merge == 1 // curasco to micaela bastidas
replace ubigeo1993 = 040511 if ubigeohost == 040520 // majes to lluta
replace ubigeo1993 = 040112 if ubigeohost == 040129 & _merge == 1 // Jose L Bust. y Rivero to paucarpata
replace ubigeo1993 = 050101 if ubigeohost == 050115 & _merge == 1 // jesus nazareno to ayacucho
replace ubigeo1993 = 050407 if ubigeohost == 050408 & _merge == 1 // llochegua to sivia
replace ubigeo1993 = 060307 if ubigeohost == 060312 & _merge == 1 // la libertad de pallan to miguel iglesias
replace ubigeo1993 = 060413 if ubigeohost == 060419 & _merge == 1 // chalamarca to paccha
replace ubigeo1993 = 080806 if ubigeohost == 080808 & _merge == 1 // alto pichigua to pichigua
replace ubigeo1993 = 080907 if ubigeohost == 080910 & _merge == 1 // pichari to kimbiri
replace ubigeo1993 = 090101 if ubigeohost == 090118 & _merge == 1 // ascension to huancavelica
replace ubigeo1993 = 100302 if ubigeohost == 101104 // aparicio pomares to chupa
replace ubigeo1993 = 100306 if ubigeohost == 101108 // choras to chavinillo
replace ubigeo1993 = 100102 if ubigeohost == 100111 & _merge == 1 // pillco arca to amarilis
replace ubigeo1993 = 130201 if ubigeohost == 130208 & _merge == 1 // casa grande to ascope
replace ubigeo1993 = 130112 if ubigeohost == 131202 & _merge == 1 // cguadalupito to old viru
replace ubigeo1993 = 130112 if ubigeohost == 131203 & _merge == 1 // viru to old viru
replace ubigeo1993 = 140115 if ubigeohost == 140116 & _merge == 1 // patapo to sana
replace ubigeo1993 = 140115 if ubigeohost == 140117 & _merge == 1 // pucala to sana
replace ubigeo1993 = 140115 if ubigeohost == 140119 & _merge == 1 // cayalti to sana
replace ubigeo1993 = 140101 if ubigeohost == 140118 & _merge == 1 // pomalca to cix
replace ubigeo1993 = 140101 if ubigeohost == 140120 & _merge == 1 // tuman to cix
replace ubigeo1993 = 160101 if ubigeohost == 160112  // belen to iquitos
replace ubigeo1993 = 160401 if ubigeohost == 160404 & _merge == 1 // san pablo to ramon castill
replace ubigeo1993 = 160507 if ubigeohost == 160510 & _merge == 1 // jenaro herrera to saquena
replace ubigeo1993 = 160111 if ubigeohost == 160511 // yaqyerana
replace ubigeo1993 = 170101 if ubigeohost == 170104 & _merge == 1 // laberinto to tambopata
replace ubigeo1993 = 170201 if ubigeohost == 170204 // huetetopue to manu
replace ubigeo1993 = 210504 if ubigeohost == 210505 & _merge == 1 // conduriri santa rosa
replace ubigeo1993 = 211201 if ubigeohost == 211209 & _merge == 1 // alto inambaro to sandia
replace ubigeo1993 = 230101 if ubigeohost == 230110 // albarracin to tacna
replace ubigeo1993 = 250102 if ubigeohost == 250106 & _merge == 1 // n requena to campo verde
replace ubigeo1993 = 250301 if ubigeohost == 250303 & _merge == 1 // curimana to p abad


duplicates report ubigeo1993
tab ubigeohost if ubigeo1993 == . & _merge == 1














e
sort ubigeo2008
gen u2008to1993 = ubigeo2008
order ubigeo2008 u2008 ubigeo1993 fecha

replace u2008to1993 = "010201" if u2008to1993 == "010206" // bagua to la peca
replace u2008to1993 = "021801" if u2008to1993 == "021809" // nuevo chimbote to old chimbote
replace u2008to1993 = "030202" if u2008to1993 == "030219" // nuevo chimbote to old chimbote
replace u2008to1993 = "030501" if u2008to1993 == "030506" // challhuahuacho to tambobamba
replace u2008to1993 = "030605" if u2008to1993 == "030608" // ranracancha to ocobamba
replace u2008to1993 = "030706" if u2008to1993 == "030714" // curasco to micaela bastidas
replace u2008to1993 = "040112" if u2008to1993 == "040129" // Jose L Bust. y Rivero to paucarpata

* unite many districts around majes and call them lluta (under its ubigeo)
replace u2008to1993 = "040511" if u2008to1993 == "040520" // majes to lluta
*replace u2008to1993 = "040511" if u2008to1993 == "040120" // sta i de sihuas to lluta
*replace u2008to1993 = "040511" if u2008to1993 == "040118" // s j de sihuas to lluta
*replace u2008to1993 = "040511" if u2008to1993 == "040413" // uraca to lluta
*replace u2008to1993 = "040511" if u2008to1993 == "040407" // huancarqui to lluta

replace u2008to1993 = "050101" if u2008to1993 == "050115" // jesus nazareno to ayacucho
replace u2008to1993 = "050407" if u2008to1993 == "050408" // llochegua to sivia
replace u2008to1993 = "060307" if u2008to1993 == "060312" // la libertad de pallan to miguel iglesias
replace u2008to1993 = "060413" if u2008to1993 == "060419" // chalamarca to paccha
replace u2008to1993 = "080806" if u2008to1993 == "080808" // alto pichigua to pichigua
replace u2008to1993 = "080907" if u2008to1993 == "080910" // pichari to kimbiri
replace u2008to1993 = "090101" if u2008to1993 == "090118" // ascension to huancavelica
replace u2008to1993 = "100102" if u2008to1993 == "100111" // pillco arca to amarilis
replace u2008to1993 = "100306" if u2008to1993 == "101108" // choras to chavinillo
replace u2008to1993 = "100302" if u2008to1993 == "101104" // aparicio pomares to chupa
replace u2008to1993 = "130201" if u2008to1993 == "130208" // casa grande to ascope
replace u2008to1993 = "130112" if u2008to1993 == "131201" // chao to old viru
replace u2008to1993 = "130112" if u2008to1993 == "131202" // cguadalupito to old viru
replace u2008to1993 = "130112" if u2008to1993 == "131203" // viru to old viru
replace u2008to1993 = "130609" if u2008to1993 == "131103" // cormin
replace u2008to1993 = "140115" if u2008to1993 == "140116" // patapo to sana
replace u2008to1993 = "140115" if u2008to1993 == "140117" // pucala to sana
replace u2008to1993 = "140115" if u2008to1993 == "140119" // cayalti to sana
replace u2008to1993 = "140101" if u2008to1993 == "140118" // pomalca to cix
replace u2008to1993 = "140101" if u2008to1993 == "140120" // tuman to cix
replace u2008to1993 = "160101" if u2008to1993 == "160112" // belen to iquitos
replace u2008to1993 = "160101" if u2008to1993 == "160113" // san juan bautista to iquitos
replace u2008to1993 = "160109" if u2008to1993 == "160114" // tentiente.. to putumayo
replace u2008to1993 = "160401" if u2008to1993 == "160404" // san pablo to ramon castill
replace u2008to1993 = "160111" if u2008to1993 == "160511" // yaqyerana
replace u2008to1993 = "160507" if u2008to1993 == "160510" // jenaro herrera to saquena
replace u2008to1993 = "160201" if u2008to1993 == "160706" // andoas
replace u2008to1993 = "170101" if u2008to1993 == "170104" // laberinto to tambopata
replace u2008to1993 = "170201" if u2008to1993 == "170204" // manu to madre de dios
replace u2008to1993 = "210504" if u2008to1993 == "210505" // conduriri santa rosa
replace u2008to1993 = "211201" if u2008to1993 == "211209" // alto inambaro to sandia
replace u2008to1993 = "211207" if u2008to1993 == "211210" // putina to san juan
replace u2008to1993 = "230101" if u2008to1993 == "230110" // albarracin to tacna
replace u2008to1993 = "240201" if u2008to1993 == "240203" // albarracin to tacna
replace u2008to1993 = "250101" if u2008to1993 == "250107" // manantay to calleria
replace u2008to1993 = "250102" if u2008to1993 == "250106" // n requena to campo verde
replace u2008to1993 = "250301" if u2008to1993 == "250303" // curimana to p abad

replace ubigeo1993 = u2008to1993 if new == 1
replace ubigeo1993 = u2008to1993 if ubigeo1993 == ""

drop _merge
merge m:1 ubigeo1993 using `temp'
keep if _merge == 3

keep ubigeo2008 ubigeo1993

** SAVE
rename ubigeo2008 ubigeo1995
save `working'/ubigeo1995to1993, replace
